package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.TravelComment;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.repository.TravelCommentRepository;
import cn.wolfcode.wolf2w.service.ITravelCommentService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;


@Service
public class TravelCommentServiceImpl implements ITravelCommentService {

    @Autowired
    private TravelCommentRepository repository;

    @Autowired
    private MongoTemplate template;

    @Override
    public void save(TravelComment comment) {
        comment.setId(null);
        repository.save(comment);
    }

    @Override
    public void update(TravelComment comment) {
        repository.save(comment);

    }


    @Override
    public TravelComment get(String id) {
        return repository.findById(id).orElse(null);
    }

    @Override
    public void addComment(TravelComment comment,UserInfo userInfo) {
        // 维护数据
        // userId
        comment.setUserId(userInfo.getId());
        // nickname city level headImgUrl
        BeanUtils.copyProperties(userInfo,comment);
        // userInfo 中的id将会覆盖掉评论的id 所以需要额外处理,我们将在业务的新增方法处理

        //createTime
        comment.setCreateTime(new Date());

        if (comment.getType() == 1){ // 判断评论的类别 1为评论中的评论
            String rId = comment.getRefComment().getId(); // 获取评论中的评论的id
            TravelComment refComment = this.get(rId);
            comment.setRefComment(refComment);  // 存入评论中的评论
        }
        this.save(comment);

    }
}
